package com.jackrain.nea.clould.dao;

import com.jackrain.nea.clould.model.SendMonitorModel;
import com.jackrain.nea.util.Tools;
import com.jackrain.nea.web.query.QueryEngine;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.sql.Timestamp;

/**
 * 消费者
 */
@Slf4j
@Component
public class SendMonitorDao {


    public int insert(SendMonitorModel sendMonitorModel) throws Exception {

        if (sendMonitorModel.getId() <= 0) {
            sendMonitorModel.setId(Tools.getInt(com.jackrain.nea.core.db.Tools.getSequence("ons_sendmonitor"), 0));
        }

        String sql = "insert into ons_sendmonitor " +
                "(id,ad_client_id,ptype,topicid,tag, " +
                "msgkey,state,consumer_error,datacount,xqty,pdesc," +
                "ownerid,modifierid,creationdate,modifieddate,isactive)" +
                "values(?,?,?,?,?," +
                "?,?,?,?,?,?," +
                "?,?,?,?,?) ";

        Timestamp time = new Timestamp(System.currentTimeMillis());

        int ret = QueryEngine.getInstance().executeUpdate(sql, new Object[]{
                sendMonitorModel.getId(), sendMonitorModel.getAd_client_id(), sendMonitorModel.getPtype(), sendMonitorModel.getTopicid(), sendMonitorModel.getTag(),
                sendMonitorModel.getMsgkey(), sendMonitorModel.getState(), sendMonitorModel.getConsumer_error(), sendMonitorModel.getDatacount(), sendMonitorModel.getXqty(), sendMonitorModel.getPdesc(),
                893, 893, time, time, "Y"});


        return ret;

    }

    public int updateState(String state, String pdesc, String msgID, String msgKey) {

        String sql = "update ons_sendmonitor set state= ?,pdesc=?,msgid=? where msgkey = ?";

        int ret = QueryEngine.getInstance().executeUpdate(sql, new Object[]{state, pdesc, msgID, msgKey});

        return ret;

    }

    public int updateState(String state, String pdesc, String msgKey) {

        String sql = "update ons_sendmonitor set state= ?,pdesc=? where msgkey = ?";

        int ret = QueryEngine.getInstance().executeUpdate(sql, new Object[]{state, pdesc, msgKey});

        return ret;

    }


    public int updateStateIsErr(String state, String isConsumerErr, String msgKey) {

        String sql = "update ons_sendmonitor set state= ?,CONSUMER_ERROR=? where msgkey = ?";

        int ret = QueryEngine.getInstance().executeUpdate(sql, new Object[]{state, isConsumerErr, msgKey});

        return ret;

    }

    public int updatePDesc(String pdesc, String msgKey) {

        String sql = "update ons_sendmonitor set pdesc= ? where msgkey = ?";

        int ret = QueryEngine.getInstance().executeUpdate(sql, new Object[]{pdesc, msgKey});

        return ret;

    }

    public int updateDataCount(int datacount, int xqty, String msgKey) {

        String sql = "update ons_sendmonitor set datacount= ?,xqty=? where msgkey = ?";

        int ret = QueryEngine.getInstance().executeUpdate(sql, new Object[]{datacount, xqty, msgKey});

        return ret;

    }

    public int delete(Timestamp beginDate) {

        String sql = "delete from ons_sendmonitor where modifieddate < ?";

        int ret = QueryEngine.getInstance().executeUpdate(sql, new Object[]{beginDate});

        return ret;
    }

    public int queryID(String msgKey) throws Exception {

        String sql = "select id " +
                " from ons_sendmonitor" +
                " where msgkey = ?";

        int ret = Tools.getInt(QueryEngine.getInstance().doQueryOne(sql, new Object[]{msgKey}), 0);

        return ret;
    }


}
